
require "achieveCfg"


local uf_bg = UIInfo.create()
local uf_itemname = UIInfo.create()
local uf_progress = UIInfo.create()
local uf_progressDesc = UIInfo.create()
local uf_award = UIInfo.create()
local uf_awardCnt = UIInfo.create()
local uf_ok = UIInfo.create()
local uf_going = UIInfo.create()
local uf_finished = UIInfo.create()
local uf_desc = UIInfo.create()


local uf_lvbg = UIInfo.create()
local uf_lv1 = UIInfo.create()
local uf_lv2 = UIInfo.create()
local uf_lv3 = UIInfo.create()
local uf_lv4 = UIInfo.create()
local uf_recommend = UIInfo.create()

local coinFnt = pp.fnt_white_pty


AchievePanel = class("AchievePanel")

function AchievePanel.create(parent)
    local panel = AchievePanel.new()
    panel:init(parent)

    panel.view:setPosition(pp.center.x, pp.center.y)
    uiEffect.bounceIn(panel.view)    
    parent:base_onOpenWin(panel)
    
    return panel
end

function AchievePanel:init(parent)
    self.view = ccs.GUIReader:getInstance():widgetFromJsonFile("achievePanel.json")
    parent.widget:addChild(self.view)
    self.parent = parent
    self.view:setLocalZOrder(pp.z_intensify)

    local function btCloseCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn2)

        elseif eventType == ccui.TouchEventType.ended then
            
            uiEffect.bounceOut(self.view, true)
            self.parent:base_onCloseWin(self)
         end
    end
    local bt_close = self.view:getChildByName("_close")
    bt_close:addTouchEventListener(btCloseCallback)
    
 
    self.list = self.view:getChildByName("_list")
    local protoItem = self.list:getChildByName("bg1")
    uf_bg:readInfoFromNode(protoItem)

    do
        local ui = protoItem:getChildByName("_item_name")
        uf_itemname:readInfoFromNode(ui)
    end
    
    do
        local ui = protoItem:getChildByName("_progress")
        uf_progress:readInfoFromNode(ui)
    end
    
    do
        local ui = protoItem:getChildByName("_progress_desc")
        uf_progressDesc:readInfoFromNode(ui)
    end

    do
        local ui = protoItem:getChildByName("_award")
        uf_award:readInfoFromNode(ui)
    end

    do
        local ui = protoItem:getChildByName("_award_cnt")
        uf_awardCnt:readInfoFromNode(ui)
    end

    do
        local ui = protoItem:getChildByName("_ok")
        uf_ok:readInfoFromNode(ui)
    end

    do
        local ui = protoItem:getChildByName("_going")
        uf_going:readInfoFromNode(ui)
    end

    do
        local ui = protoItem:getChildByName("_finished")
        uf_finished:readInfoFromNode(ui)
    end


    do
        local ui = protoItem:getChildByName("_desc")
        uf_desc:readInfoFromNode(ui)
    end

    protoItem:removeFromParent(true)

    ---------------------------------------------------

    self.itemInfo = {}

    for k,v in ipairs(achieveCfg.orderInList) do
        self.itemInfo[#self.itemInfo+1] = {
            progress = nil,
            progressDesc = nil,
            award = nil,
            awardcnt = nil
        }
        
        local item = achieveCfg[v]

        local bg = ccui.ImageView:create("common/di1.png", ccui.TextureResType.localType)
        uf_bg:setInfoToNode(bg)

        --itemname
        local itemname = ccui.ImageView:create(item.nameImg, ccui.TextureResType.localType)
        uf_itemname:setInfoToNode(itemname)
        bg:addChild(itemname)

        --desc
        local appendant,_descText
        if item.bNeedAppendant then
            appendant = achieveCfg.calc_appendant(item)
            _descText = string.format(item.descText,appendant )
        else
            _descText = item.descText
        end 
                                
--        local desc = cc.LabelBMFont:create(_descText, pp.fnt_com)
--        uf_desc:setInfoToNode(desc)
--        bg:addChild(desc)

        --score progress
        self.itemInfo[k].progress = cc.ProgressTimer:create(cc.Sprite:create("achieve/jingdu.png"))
        self.itemInfo[k].progress:setType(cc.PROGRESS_TIMER_TYPE_BAR)
        self.itemInfo[k].progress:setMidpoint(cc.p(0, 0))
        self.itemInfo[k].progress:setBarChangeRate(cc.p(1, 0))

        uf_progress:setInfoToNode(self.itemInfo[k].progress)
        bg:addChild(self.itemInfo[k].progress)
        --------------------------------------

        --progress desc
        self.itemInfo[k].progressDesc = cc.LabelBMFont:create("00", pp.fnt_number_white)
        uf_progressDesc:setInfoToNode(self.itemInfo[k].progressDesc)

        --award
        self.itemInfo[k].award = ccui.ImageView:create(pp.gameObjs.coin.img, ccui.TextureResType.localType)
        uf_award:setInfoToNode(self.itemInfo[k].award)
        bg:addChild(self.itemInfo[k].award)
        
        --award cnt
        self.itemInfo[k].awardcnt = cc.LabelBMFont:create("100", pp.fnt_number_white)
        uf_awardCnt:setInfoToNode(self.itemInfo[k].awardcnt)
        bg:addChild(self.itemInfo[k].awardcnt)
      






      
        --recommend
--        if v.bRecommend then
--            local rec = ccui.ImageView:create("common/srcres/jian.png", ccui.TextureResType.localType)
--            uf_recommend:setInfoToNode(rec)
--            uiEffect.blink(rec)
--            bg:addChild(rec)
--        end
--
--        local bt = ccui.Button:create("common/shengji.png")
--        uf_ok:setInfoToNode(bt)
--        bg:addChild(bt)
--        self.itemInfo[k].bt = bt
--        bt:addTouchEventListener(btCallbacks[k])
--
--
--        local coinfnt = cc.LabelBMFont:create(v.coin[k],  pp.fnt_number_yellow)
--        uf_coin_fnt:setInfoToNode(coinfnt)
--        coinfnt:setScale(0.45)  
--        bt:addChild(coinfnt)
--        self.itemInfo[k].coin = coinfnt
--
--        local coin = ccui.ImageView:create("common/jinbi.png", ccui.TextureResType.localType)
--        uf_coin:setInfoToNode(coin)
--        bt:addChild(coin)
--        
--
--        --fnt
--        uiBMFont:setAddInfo(bg, 10)
--        uiBMFont:setColor(cc.c3b(205,233,255))
--        uiBMFont:createMultiLines( v.desc, pp.fnt_qianghua, 0.75, 200, uf_desc.position.x, uf_desc.position.y, 22)

        self.list:addChild(bg)
    end

--    self:refreshByUserData()
end